package Control;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EditPath {
	public String editPath(String commandLine)
	{
		String[] cmd = commandLine.split(" ");
		if ((!"cd".equals(cmd[0])) && (!Controler.currentPath.isEmpty()))
		{
			return replacePath(commandLine, Controler.currentPath);
		}
		return commandLine;
	}
	
	private String replacePath(String cmd, String path)
	{
		StringBuilder result = new StringBuilder(cmd);
		String tmp="";
		String[] str = cmd.split(" ");
		Pattern template = Pattern.compile("'.*'");
		Matcher match;
		for (int i=0;i<str.length;i++)
		{
			match = template.matcher(str[i]);
			if (match.matches())
			{
				tmp = path+"/"+str[i].substring(1,str[i].length()-1);
				result.replace(cmd.indexOf(str[i]),cmd.indexOf(str[i])+str[i].length() , tmp);
			}
		}
		System.out.println("path " + path+" ");
		System.out.println("tmp " + tmp+" ");
		System.out.println("result " + result);
		return result.toString();
	}
}
